package com.hllxd.hism.star.basic.service.dao.redis;

import com.hllxd.hism.star.basic.service.config.redis.RedisAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author lixiyong01
 * @date 2021-08-21 11:29
 *
 * 写入数据，由bibcp-counter读取使用
 */
@Slf4j
@Service("confRedisMapper")
public class ConfRedisMapperImpl implements ConfRedisMapper {
    @Autowired private RedisAdapter redisAdapter;

    private static String buildKey(String k) {
        return "bibcp^counter^conf^" + k;
    }

    @Override
    public Map<Object, Object> readConfig(String appId) {
        String key = buildKey(appId);
        return redisAdapter.hgetall(key);
    }

    @Override
    public String readItem(String appId, String hkey) {
        String key = buildKey(appId);
        Object data = redisAdapter.hget(key, hkey);
        if(data != null) {
            return (String)data;
        }
        return null;
    }

    @Override
    public Boolean setItem(String appId, String hkey, String value) {
        try {
            String key = buildKey(appId);
            redisAdapter.hset(key, hkey, value);
        }catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean delItem(String appId, String hkey) {
        try {
            String key = buildKey(appId);
            redisAdapter.hdel(key, hkey);
        }catch (Exception e) {
            return false;
        }
        return true;
    }
}
